﻿using System;
using HNAlbum.Class;
using HNAlbum.Component.AlbumGallery;
using HNAlbum.Component.NivoSlider;
using HNAlbum.WebControl;
using System.Web.Services;
using HNAlbum.Logic.Entity;
using HNAlbum.Logic.Manager;

namespace HNAlbum
{
    /// <summary>
    /// View album page
    /// </summary>
    public partial class Album : BasePage
    {
        #region Private fields
        /// <summary>
        /// Album to load
        /// </summary>
        private string AlbumID
        {
            get { return (Request.QueryString["id"] != null) ? Request.QueryString["id"] : string.Empty; }
        }
        /// <summary>
        /// Album page to load
        /// </summary>
        private int AlbumPage
        {
            get { return (Request.QueryString["page"] != null) ? int.Parse(Request.QueryString["page"]) : 1; }
        }
        #endregion

        #region Page events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            //Register CSS
            RegisterCSS("jquery.nivo.slider.css");
            RegisterCSS("jquery.nivo.slider.hnalbum.css");

            //Register JS
            RegisterJS("Album.js");
            RegisterJS("jquery.nivo.slider.pack.js", "");

            //Register page size to process on client side
            ClientScript.RegisterHiddenField("hdePageSize", NivoSlider.PHOTO_PER_PAGE.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check for album availability
            if (string.IsNullOrWhiteSpace(AlbumID))
            {
                SetPageMessage("Album not found.", PageMessage.MessageType.Error);
            }
            else
            {
                //TODO: Temp commented
                /*
                //Guest try to "hack" the query string value
                if (IsGuest && !AlbumID.Equals(AlbumManager.DEMO_ALBUM))
                {
                    //SetPageMessage("Album not found.", PageMessage.MessageType.Error);
                    //return;
                }
                else
                {*/
                    try
                    {
                        NivoSlider slider = new NivoSlider(AlbumID, AlbumPage);
                        divSlider.Controls.Add(slider);
                        SetPageMessage("Album is being built. Please wait...", PageMessage.MessageType.Info);

                        //Then create an instance of the pager based on the dashboard
                        DashboardPager pager = new DashboardPager(NivoSlider.PHOTO_PER_PAGE, slider.PhotoCount, AlbumPage, string.Format("Home.aspx?page={0}", CustomMasterPage.SessionCollection.Page.DashboardPage));
                        pnlPager.Controls.Add(pager);
                    }
                    catch (Exception ex)
                    {
                        //TODO: Log exceptions
                        Console.Write(ex.Message);
                        SetPageMessage("Album not found.", PageMessage.MessageType.Error);
                    }
                //}
            }
        }
        #endregion

        #region Web methods
        [WebMethod]
        public static string PrepareSourcePhoto(string albumId, string photoId)
        {
            HNAlbumData album = new HNAlbumData(albumId);
            album.Load();
            HNAlbumPhoto photo = album.PhotoCollection[photoId];

            string srcUrl = photo.CreateTempSource();
            return "{\"url\":\"" + srcUrl + "\"}";
        }
        #endregion
    }
}